package cn.kooki.app.duobao.ui.widget;

import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.kooki.app.duobao.R;
import cn.kooki.app.duobao.data.Bean.Index.GoodsItem;
import cn.kooki.app.duobao.data.bus.ChangePageEvent;
import cn.kooki.app.duobao.util.ScreenUtils;
import cn.kooki.app.duobao.util.manager.ShopCartManager;
import de.greenrobot.event.EventBus;

/**
 * Created by xiong on 2015/12/15.
 */
public class ActionBarMenu extends PopupWindow implements View.OnClickListener {

    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.layout_goCart)
    RelativeLayout layoutGoCart;
    @Bind(R.id.layout_goIndex)
    RelativeLayout layoutGoIndex;
    private AppCompatActivity context;


    private ActionBarMenuClickListener listener;

    public void setActionMenuListener(ActionBarMenuClickListener listener) {
        this.listener = listener;
    }

    public interface ActionBarMenuClickListener {
        void onItemClick(int index);
    }

    public ActionBarMenu(AppCompatActivity context) {
        this.context = context;
        initView();
    }


    private void initView() {
        View view = View.inflate(context, R.layout.popup_menu_topright, null);
        ButterKnife.bind(this, view);
        setContentView(view);
        setBackgroundDrawable(new BitmapDrawable());
        setFocusable(true);
        setWidth(ScreenUtils.dpToPxInt(context,120));
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        refreshCount();
        layoutGoCart.setOnClickListener(this);
        layoutGoIndex.setOnClickListener(this);
    }

    public void refreshCount() {
        int size = ShopCartManager.getInstance().getCartsSize();
        tvNum.setText(size + "");
        tvNum.setVisibility(size == 0 ? View.GONE : View.VISIBLE);
    }


    private ViewGroup findSuitableParent(View view) {
        ViewGroup fallback = null;

        do {
            if (view instanceof CoordinatorLayout) {
                return (ViewGroup) view;
            }

            if (view instanceof FrameLayout) {

                fallback = (ViewGroup) view;
            }

            if (view != null) {
                ViewParent parent = view.getParent();
                view = parent instanceof View ? (View) parent : null;
            }
        } while (view != null);

        return fallback;
    }

    public void unbind() {
        ButterKnife.unbind(this);
    }


    @Override
    public void onClick(View v) {
        if (layoutGoIndex == v) {

            if (listener != null) {
                dismiss();
                listener.onItemClick(1);

            }

        }
        if (layoutGoCart == v) {
            if (listener != null) {
                dismiss();
                listener.onItemClick(0);
            }

        }
    }
}
